<?php

function save_data($title, $content, $fid, $uid, $is_aid){
 	
	$uid = rand(3, 16); //3-16可用UID

 	$time = time();
 	$author = mysql_fetch_array(mysql_query("select * from pw_user WHERE uid = '$uid'"));
 	

    $cf_result = mysql_query("select * from pw_bbs_threads WHERE subject = '$title'");//重复性
    if (mysql_num_rows($cf_result)) {
    	$msg = $title.'——重复文章跳过。。。<br/>';	
    }else{

    	//是否有附件
    	if ($is_aid == 0) {
    		$ifupload = 0;
    		$aids = 0;
    	}else{
    		$ifupload = 1;
    		$aids = 1;
    	}

	    //pw_bbs_threads
		$sql = "INSERT INTO pw_bbs_threads (fid, subject, ischeck, ifupload, created_time, created_username, created_userid, created_ip, lastpost_time, lastpost_userid, lastpost_username, reply_notice)
		VALUES ('$fid', '$title', '1', '$ifupload', '$time', '$author[username]', '$uid', '127.0.0.1', '$time', '$uid', '$author[username]', '1')";
	    if (mysql_query($sql)) {
	    	$rs = mysql_fetch_array(mysql_query("select * from pw_bbs_threads ORDER BY tid DESC"));
	    	$tid = $rs['tid'];

	    	//pw_bbs_threads_content
	    	$post_sql = "INSERT INTO pw_bbs_threads_content (tid, useubb, aids, content, ipfrom)
			VALUES ('$tid', '1', '$aids', '$content', '本机地址')";

			if (mysql_query($post_sql)) {

				//pw_bbs_threads_index
		    	$sql = "INSERT INTO pw_bbs_threads_index (tid, fid, created_time, lastpost_time)
				VALUES ('$tid', '$fid', '$time', '$time')";
				mysql_query($sql);

				//pw_bbs_threads_hits
		    	$sql = "INSERT INTO pw_bbs_threads_hits (tid, hits)
				VALUES ('$tid', '1')";
				mysql_query($sql);

				$cid = mysql_fetch_array(mysql_query("select * from pw_bbs_forum WHERE fid = '$fid'"));
				$cid = $cid['parentid'];
				//pw_bbs_threads_cate_index
		    	$sql = "INSERT INTO pw_bbs_threads_cate_index (tid, cid, fid, created_time, lastpost_time)
				VALUES ('$tid', '$cid', '$fid', '$time', '$time')";
				mysql_query($sql);

				//UPDATE pw_bbs_forum_statistics
				$sql = "UPDATE pw_bbs_forum_statistics SET todayposts = todayposts+1, article = article+1, threads = threads+1, 
						lastpost_info = '$title', lastpost_time = '$time', lastpost_username = '$author[username]', lastpost_tid = '$tid' WHERE fid = '$cid'";
				mysql_query($sql);

				//UPDATE FID
				$sql = "UPDATE pw_bbs_forum_statistics SET todayposts = todayposts+1, article = article+1, threads = threads+1, subthreads = subthreads+1, 
						lastpost_info = '$title', lastpost_time = '$time', lastpost_username = '$author[username]', lastpost_tid = '$tid' WHERE fid = '$fid'";
				mysql_query($sql);

				//USER 表操作 积分相关
				mysql_query("UPDATE pw_user_data SET lastpost= '$time',postnum = postnum+1, todaypost = todaypost+1, credit1 = credit1+2, credit2 = credit2+2 
				WHERE uid = '$uid'");

				mysql_query("UPDATE pw_windid_user_data SET credit1 = credit1+2, credit2 = credit2+2 WHERE uid = '$uid'");

				//如果有图片下载至本地（附件）
				if ($is_aid != 0) {
					mysql_query("UPDATE pw_attachs_thread SET tid = '$tid', name = 'IMAGE' WHERE name = '0'");
				}

				$msg = "$title ---- 已成功发布!<br/>";
			}else{
				$msg = '连接出错 pw_bbs_threads_content 表，未发布。跳过'.mysql_error()."<br/>";
			}

	    }else{
	    	$msg = '连接出错 pw_bbs_threads 表，未发布。跳过'.mysql_error()."<br/>";
	    }

	}


	return $msg;
}